package com.bw.module_service.ui.polling

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.Button
import android.widget.GridLayout
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.databinding.DataBindingUtil.setContentView
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.bw.lib_common.arouter.Router
import com.bw.lib_common.base.BaseActivity
import com.bw.module_service.R
import com.bw.module_service.adapter.TaskAdapter
import com.bw.module_service.databinding.ActivityPatrolInspectionBinding
import com.bw.module_service.model.TaskEntity
import com.bw.module_service.viewmodel.ServiceViewModel
import dagger.hilt.android.AndroidEntryPoint
import java.util.Calendar
@AndroidEntryPoint
@Route(path = Router.Ui.PatrolInspectionActivity)
class PatrolInspectionActivity : BaseActivity<ServiceViewModel,ActivityPatrolInspectionBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_patrol_inspection
    private lateinit var taskAdapter: TaskAdapter
    private val mViewModel: ServiceViewModel by viewModels()
    override fun initData() {
        mViewModel.getTaskList()
        mViewModel.taskListLiveData_.observe(this){
           taskAdapter.data.clear()
            taskAdapter.data.addAll(it)
            taskAdapter.notifyDataSetChanged()
        }
    }
    @SuppressLint("ResourceAsColor")
    override fun initView() {
        mBinding.back.setOnClickListener {
            finish()
        }
        mBinding.rv.layoutManager= LinearLayoutManager(this)
        taskAdapter= TaskAdapter()
        mBinding.rv.adapter=taskAdapter
        val calendar = Calendar.getInstance()
        val daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH) // 获取月份的天数
        val firstDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK)+1 // 获取第一天的星期
        //当前的年月
        val currentMonth = calendar.get(Calendar.MONTH) +1
        val currentYear = calendar.get(Calendar.YEAR)
        val date = calendar.get(Calendar.DATE)
        calendar.set(currentYear, Calendar.MONTH, 1)
        // 月份选择器
        mBinding.time.text = "${currentYear}年${currentMonth}月"

        // 添加日期到网格
        for (i in 1..daysInMonth) {
            val dateButton = Button(this)
            dateButton.text = i.toString()
            if (date==i){
                dateButton.setBackgroundResource(R.color.blue)
            }
            dateButton.layoutParams = GridLayout.LayoutParams().apply {
                width = 0
                height = GridLayout.LayoutParams.WRAP_CONTENT
                columnSpec = GridLayout.spec((i + firstDayOfWeek - 2) % 7, 1f) // 列权重为 1
                rowSpec = GridLayout.spec((i + firstDayOfWeek - 2) / 7)
                setMargins(4, 4, 4, 4)
            }
//            dateButton.setOnClickListener {
//                // 处理日期点击事件
//                Toast.makeText(this, "选中日期: $i", Toast.LENGTH_SHORT).show()
//            }
            mBinding.dateGrid.addView(dateButton)
        }

    }



    override fun initListener() {

    }
}